package com.zzvcom.template;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import com.zzvcom.utils.TagUtils;

public class ResultPage
{
    public static final int PAGE_SIZE = 10;

    private int currentPage = 1;

    private int pageSize = PAGE_SIZE;

    private int totalPage = 0;

    private int totalRecord = 0;

    private List result = new ArrayList();

    private DetachedCriteria detachedCriteria;

    private String hql;

    private Object[] hqlParams;

    private String countHql;

    private Set<Order> orders = new LinkedHashSet<Order>();
    
    public void startLimitSetting(Integer start ,Integer limit)
    {
        if (start != null)
            this.setCurrentPage(start / limit + 1);
        if (limit != null)
            this.setPageSize(limit);
    }

    public int getFirstResult()
    {
        return (currentPage - 1) * pageSize;
    }

    public DetachedCriteria getDetachedCriteria()
    {
        return detachedCriteria;
    }

    public void setDetachedCriteria(DetachedCriteria detachedCriteria)
    {
        this.detachedCriteria = detachedCriteria;
    }

    public String getHql()
    {
        return hql;
    }

    public Object[] getHqlParams()
    {
        return hqlParams;
    }

    public String getCountHql()
    {
        return countHql;
    }

    /**
     * 函数介绍： 自己提供总记录数sql 分页查询 参数： 返回值：
     */
    public void setHql(String hql, String countHql, Object[] params)
    {
        this.hql = hql;
        this.hqlParams = params;
        this.countHql = countHql;
    }

    /**
     * 函数介绍： 从主句分离得到总记录数的sql分页查询 参数： 返回值：
     */

    public void setHql(String hql, Object[] params)
    {
        this.hql = hql;
        this.hqlParams = params;
        this.countHql = "select count(*) " + TagUtils.parseHSLConditions(hql);
    }

    public int getCurrentPage()
    {
        return currentPage;
    }

    public void setCurrentPage(int currentPage)
    {
        this.currentPage = currentPage;
    }

    public int getPageSize()
    {
        return pageSize;
    }

    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }

    public int getTotalPage()
    {
        return totalPage;
    }

    public void setTotalPage(int totalPage)
    {
        this.totalPage = totalPage;
    }

    public int getTotalRecord()
    {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord)
    {
        this.totalRecord = totalRecord;
    }

    public List getResult()
    {
        return result;
    }

    public void setResult(List result)
    {
        this.result = result;
    }

    public Set<Order> getOrders()
    {
        return orders;
    }

    public void setOrders(Set<Order> orders)
    {
        this.orders = orders;
    }

    public void addOrder(Order order)
    {
        this.orders.add(order);
    }

    public void removeOrder(Order order)
    {
        this.orders.remove(order);
    }

    public boolean isFirst()
    {
        return this.currentPage <= 1;
    }

    public boolean isLast()
    {
        return this.currentPage >= this.totalPage;
    }

}
